<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <link rel="stylesheet" href="../public/css/global.css">
    <link rel="stylesheet" href="../public/css/login.css">
</head>

<body>
    <div class="formContainer">
        <h1>
            欢迎注册博客
        </h1>
        <!-- 表单有默认的提交行为，此行为是同步提交，会造成浏览器锁死（转圈）等待服务器的响应结果 -->
        <form id="register_form">
            <div class="email">
                <label for="email">邮箱:</label>
                <input type="text" name="email" id="email" placeholder="请输入您的email" required>
            </div>
            <div class="nickname">
                <label for="nickname">昵称:</label>
                <input type="text" name="nickname" id="nickname" placeholder="请输入您的昵称" required>
            </div>
            <div class="password">
                <label for="password">密码:</label>
                <input type="password" name="password" id="password" placeholder="请输入您的密码" required>
            </div>
            <button type="submit">注册</button>
        </form>
        <p>已有账号 <a href="/login">点击登录</a></p>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
    <script>
        // 绑定submit事件
        $('#register_form').on('submit', function (e) {
            // 阻止默认行为
            e.preventDefault()
            let formData = $(this).serialize()
            // 发送ajax请求
            $.ajax({
                url: '/register',
                type: 'post',
                data: formData,
                dataType: 'json',
                success: function (data) {
                    if(data.err_code === 0){
                        window.alert('注册成功，快去登录吧！')
                        window.location.href = '/login'
                    }else if(data.err_code === 1){
                        window.alert('邮箱或昵称已存在')
                    }else{
                        window.alert(data.msg)
                    }
                }
            })
        })
    </script>
</body>

</html>